import QtQuick 1.0
import InvMA 1.0
import "Magnifier.js" as MagnifierPopup

FocusScope {
    id: root

    property alias font: textEdit.font

    property alias cursorPosition: textEdit.cursorPosition
    property alias text: textEdit.text
    property alias readOnly: textEdit.readOnly
    property alias textFormat: textEdit.textFormat

    property alias wrapMode: textEdit.wrapMode

    signal textChanged
    signal cursorRectangleChanged

    property alias cursorRectangle: textEdit.cursorRectangle

    property alias tx: textEdit.x
    property alias ty: textEdit.y

    function copy() {
        textEdit.copy()
    }

    function paste() {
        textEdit.paste()
    }

    function cut() {
        textEdit.cut()
    }

    function positionAt(x, y) {
        var p = mapToItem(textEdit, x, y);
        return textEdit.positionAt(p.x, p.y)
    }

    onActiveFocusChanged: {
        if (activeFocus && !readOnly) {
            textEdit.openSoftwareInputPanel();
            //repositionTimer.running = true;
        } else if (!activeFocus) {
            if (!readOnly) {
                textEdit.closeSoftwareInputPanel();
            }
        }
    }

    /*Rectangle {
        id: wrapperTE
        //anchors.fill: parent
        anchors.top: parent.top
        anchors.left: parent.left
        width: parent.width
        height: parent.height
        color: "white"
        border.color: "blue"
        radius: 5
    }*/
    TextEdit {
        id: textEdit
        clip: true
        anchors.top: parent.top;
        anchors.left: parent.left; anchors.leftMargin: 5
        width: parent.width - 2*anchors.leftMargin
        height: parent.height
        font.pixelSize: 28

        //FIXME:
        function updateMagnifierPosition(posX, posY) {
            var magnifier = MagnifierPopup.popup;
            var cursorHeight = textEdit.positionToRectangle(0,0).height;
            var mappedPos =  mapToItem(magnifier.parent, posX - magnifier.width / 2,
                                       posY - magnifier.height / 2 - cursorHeight - 70);

            magnifier.xCenter = mapToItem(magnifier.sourceItem, posX, 0).x / magnifier.sourceItem.width;
            magnifier.x = mappedPos.x;
            if (-root.mapFromItem(magnifier.__rootElement(), 0,0).y - (posY - cursorHeight) < (magnifier.height / 1.5)) {
                magnifier.yAdjustment = Math.max(0,(magnifier.height / 1.5) + root.mapFromItem(magnifier.__rootElement(), 0,0).y - (posY - cursorHeight));
            } else {
                magnifier.yAdjustment = 0;
            }
            magnifier.yCenter = 1.0 - ((50 + posY - cursorHeight) / root.height);
            magnifier.y = mappedPos.y + magnifier.yAdjustment;
        }

        onTextChanged: {
            root.textChanged()
        }

        onCursorRectangleChanged: {
            root.cursorRectangleChanged()
        }

        InverseMouseArea {
            anchors.fill: parent
            enabled: root.activeFocus
            onClickedOutside: {
                root.parent.focus = true;
            }
        }

        //FIXME:
        MouseArea {
            anchors.fill: parent
            enabled: root.activeFocus
            onPressAndHold:{
                // possible pre-edit word have to be commited before showing the magnifier
                console.log("MouseArea: onPressAndHold")
                if ((root.text != "") && root.activeFocus) {
                    //inputContext.reset()
                    //attemptToActivate = false
                    //parent.selectByMouse = false
                    MagnifierPopup.open(root);
                    var magnifier = MagnifierPopup.popup;
                    parent.cursorPosition = parent.positionAt(mouse.x,mouse.y)
                    parent.updateMagnifierPosition(mouse.x,mouse.y)
                    root.z = Number.MAX_VALUE
                }
            }
        }
    }


}
